{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a8d03e83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 导入 unicodedata 模块中的 normalize 函数\n",
    "from unicodedata import normalize\n",
    "\n",
    "# 定义函数：使用 NFC 规范化形式比较两个字符串是否相等\n",
    "def nfc_equal(str1, str2):\n",
    "    \"\"\"\n",
    "    参数:\n",
    "        str1 (str): 第一个要比较的字符串\n",
    "        str2 (str): 第二个要比较的字符串\n",
    "    \n",
    "    返回:\n",
    "        bool: 如果两个字符串在 NFC 规范化后相同则返回 True，否则返回 False\n",
    "    \n",
    "    说明:\n",
    "        NFC (Normalization Form Canonical Composition) 规范化形式会将字符分解后再重新组合\n",
    "        例如: 'é' 可以表示为单个字符 'é'(U+00E9) 或 'e' + '´'(U+0065 + U+0301)\n",
    "        NFC 会将这两种表示规范化为统一的形式\n",
    "    \"\"\"\n",
    "    return normalize('NFC', str1) == normalize('NFC', str2)\n",
    "\n",
    "# 定义函数：使用大小写折叠和 NFC 规范化比较两个字符串是否相等\n",
    "def fold_equal(str1, str2):\n",
    "    \"\"\"\n",
    "    参数:\n",
    "        str1 (str): 第一个要比较的字符串\n",
    "        str2 (str): 第二个要比较的字符串\n",
    "    \n",
    "    返回:\n",
    "        bool: 如果两个字符串在 NFC 规范化且大小写折叠后相同则返回 True，否则返回 False\n",
    "    \n",
    "    说明:\n",
    "        1. 首先对两个字符串进行 NFC 规范化\n",
    "        2. 然后应用 casefold() 进行大小写折叠(比 lower() 更激进的大小写转换)\n",
    "        3. 比较处理后的字符串是否相同\n",
    "        4. 适用于不区分大小写的比较，能正确处理如:\n",
    "           'ß' (德语 sharp s) 会转换为 'ss'\n",
    "           'İ' (带点的 I) 会转换为 'i'\n",
    "    \"\"\"\n",
    "    return (normalize('NFC', str1).casefold() == \n",
    "            normalize('NFC', str2).casefold())\n",
    "\n",
    "\n",
    "s3 = 'Straße'\n",
    "s4 = 'strasse'\n",
    "print(s3 == s4)\n",
    "print(nfc_equal(s3, s4))\n",
    "print(fold_equal(s3, s4))\n",
    "print(fold_equal('A', 'a'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8bb4404f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['中', '人', '共', '华', '和', '国', '民']\n"
     ]
    }
   ],
   "source": [
    "a=[\"中\",\"华\",\"人\",\"民\",\"共\",\"和\",\"国\"]\n",
    "print(sorted(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "665c5f66",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['中', '人', '共', '华', '和', '国', '民']\n"
     ]
    }
   ],
   "source": [
    "import pyuca\n",
    "coll = pyuca.Collator()\n",
    "a=[\"中\",\"华\",\"人\",\"民\",\"共\",\"和\",\"国\"]\n",
    "sorted_a=sorted(a, key=coll.sort_key)\n",
    "print(sorted_a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12375211",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CJK UNIFIED IDEOGRAPH-4E2D\n"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m在当前单元格或上一个单元格中执行代码时 Kernel 崩溃。\n",
      "\u001b[1;31m请查看单元格中的代码，以确定故障的可能原因。\n",
      "\u001b[1;31m单击<a href='https://aka.ms/vscodeJupyterKernelCrash'>此处</a>了解详细信息。\n",
      "\u001b[1;31m有关更多详细信息，请查看 Jupyter <a href='command:jupyter.viewOutput'>log</a>。"
     ]
    }
   ],
   "source": [
    "from unicodedata import name\n",
    "print(name(\"中\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
